package com.usian.common.interceptor;

import com.usian.model.media.pojos.WmUser;
import com.usian.utils.threadlocal.WmThreadLocalUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TokenRefreshInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String refresh_token = request.getHeader("refresh_token");
        String userId = request.getHeader("userId");
        if (StringUtils.isNotBlank(userId)){
            WmThreadLocalUtils.setUserId(Integer.parseInt(userId));
        }
        if (StringUtils.isNotBlank(refresh_token)){
            response.setHeader("REFRESH_TOKEN",refresh_token);
        }
        return true;  //放行
    }

    /*@Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        String refresh_token = request.getHeader("refresh_token");
        if (StringUtils.isNotBlank(refresh_token)){
            response.setHeader("REFRESH_TOKEN",refresh_token);
        }
    }*/

    /*@Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        String refresh_token = request.getHeader("refresh_token");
        if (StringUtils.isNotBlank(refresh_token)){
            response.setHeader("REFRESH_TOKEN",refresh_token);
        }
    }*/
}
